import 'package:flutter/material.dart';
import 'package:flutter_map_demo/configs/map_select_config.dart';
import 'package:flutter_map_demo/configs/poi_search_base_config.dart';
import 'package:uikit/uikit.dart';
import '../../poi_search/view_model/poi_search_cell_view_model.dart';
import '../../poi_search/widget/poi_list/poi_cell.dart';

class ConfusionPointsWidget extends StatelessWidget {
  final List<POISearchCellViewModel> list;
  final String? keyword;
  final ValueChanged<int> clickIndex;
  final MapSelectConfig config;

  const ConfusionPointsWidget({
    Key? key,
    required this.list, this.keyword,
    required this.clickIndex,
    required this.config
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: MediaQuery.of(context).size.height * 0.5,
      width: MediaQuery.of(context).size.width,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(12),
      ),
      clipBehavior:Clip.hardEdge,
      child: CustomScrollView(
          slivers: _buildSections(list),
      ),
    );
  }

  List<Widget> _buildSections(List<POISearchCellViewModel> list) {
    List<Widget> slivers = [];

    slivers.add(SliverList(delegate: SliverChildBuilderDelegate(
        childCount: list.length, (context, index) {
      final e = list[index];
      return T3GestureDetector(
        onTap: () {
          clickIndex(index);
        },
        child: T3POICell(
          vm: e,
          config: genSearchConfig(),
          onSelectSubPoi: (subIndex) {
            e.selectedSubPOIIndex = subIndex;
            clickIndex(index);
          },
          keyword: keyword,
          clickShareButton: (poi) { },
        ),
      );})
    ));

    return slivers;
  }

  POISearchBaseConfig genSearchConfig() {
    var json = config.toJson();
    return POISearchBaseConfig.fromJson(json);
  }
}
